package com.common.dialect;


/**
* @ClassName: OracleDialect
* @Description: Oracle方言处理
* @author 冰上浮云
* @date 2013年12月9日 下午10:20:30
*
*/ 
public class OracleDialect extends Dialect {

	/* 
	* <p>Title: getLimitString</p>
	* <p>Description: </p>
	* @param sql
	* @param offset 当前显示最后一条数
	* @param limit   每页显示条数
	* @return
	* @see com.common.interceptor.paginationInterceptor.Dialect#getLimitString(java.lang.String, int, int)
	*/ 
	@Override
	public String getLimitString(String sql,  int offset, int limit) {
		sql = sql.trim();
		boolean isForUpdate = false;
		if (sql.toLowerCase().endsWith(" for update")) {
			sql = sql.substring(0, sql.length() - 11);
			isForUpdate = true;
		}

		StringBuffer pagingSelect = new StringBuffer(sql.length() + 100);
		
		pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
		
		pagingSelect.append(sql);
		
		pagingSelect.append(" ) row_ ) where rownum_ > "+offset+" and rownum_ <= "+(offset + limit));

		if (isForUpdate) {
			pagingSelect.append(" for update");
		}
		
		return pagingSelect.toString();
	}

}
